package com.lap.analysis.file.methods;

import com.lap.analysis.enums.Method;
import com.lap.analysis.exception.EnumException;
import com.lap.analysis.source.entity.MethodInfo;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MethodContext {

  private final Map<String, AbstractMethod> methodMap;

  public MethodContext() {
    this.methodMap =
        Stream.of(Method.values())
            .collect(Collectors.toMap(Method::getName, method -> createMethodInstance(method)));
  }

  public String execute(MethodInfo methodInfo) {
    AbstractMethod method = methodMap.get(methodInfo.getMethod());
    if (method != null) {
      return method.execute(methodInfo);
    }
    throw new EnumException("enum is not found:" + methodInfo.getMethod());
  }

  private AbstractMethod createMethodInstance(Method method) {
    switch (method) {
      case GET:
        return new GetMethod();
      case POST:
        return new PostMethod();
      case PUT:
        return new PutMethod();
      case DELETE:
        return new DeleteMethod();
      default:
        throw new EnumException("Unsupported method type: " + method);
    }
  }
}
